#include<stdio.h>
#include<cassert>
#include<unistd.h>
#include<string>
#include<vector>
#include<iostream>

const int num=5;
static int number=1;

struct channel
{
public:
    channel(int fd,pid_t id)
    :ctrlfd(fd),workid(id)
    {
        name="channel"+std::to_string(number++);
    }
public:
    int ctrlfd;
    pid_t workid;
    std::string name;

};

void Work()
{
    while(true)
    {
        std::cout<<"I am running"<<getpid()<<std::endl;
    }
}

void CreateChannels(std::vector<channel>* c)
{
    for(int i=0;i<num;i++)
    {
        //定义并创建管道
        int fd[2];
        int n=pipe(fd);
        assert(n==0);
        //void(n);

        //创建进程
        pid_t id=fork();
        assert(id!=-1);

        //构建单向通信信道
        if(id==0)//child读
        {
            close(fd[1]);
            //TODO
            //Work();
            

            exit(0);
        }

        //father写
        close(fd[0]);

        //childid,pipefd[1]
        c->push_back(channel(fd[1],id));
        
    }
}

void PrintDebug(const std::vector<channel>& c)
{
    for(const auto& channel:c)
    {
        std::cout<<channel.name<<","<<channel.ctrlfd<<","<<channel.workid<<std::endl;
    }
}
int main()
{
    std::vector<channel> channels;
    CreateChannels(&channels);

    //开始完成任务 




    //PrintDebug(channels);
    //sleep(10);
    return 0;
}